var ctx;
var orderNumber = "";
var state = -1;
var beginDate = "";
var endDate = "";
var pSize;
var page;

function setCtx(ctx){
	this.ctx = ctx;
}

function ifNull(mess){
	if(mess == null || mess == undefined){
		mess = "";
	}
	
	return mess;
}
/**
 * 检查日期
 * @returns {Boolean}
 */
function checkDate () {
	beginDate = $("#beginDate").val();
	endDate = $("#endDate").val();
	//解决ie9以下placeholder与value重复的问题
	if(beginDate == $("#beginDate").attr("placeholder")){
		beginDate = "";
	}
	if(endDate == $("#endDate").attr("placeholder")){
		endDate = "";
	}
    var start=new Date(beginDate.replace("-", "/").replace("-", "/"));  
    var end = new Date(endDate.replace("-", "/").replace("-", "/"));  
    
    if(end < start){
    	layer.msg("结束时间应大于起始时间",{
			icon: 3,
			time: 2000 //2秒关闭（如果不配置，默认是3秒）
		});
    	return false;
    }else{
    	return true;
    }
}

//跳转到上一页或者下一页
function gotoPage(page){
	orderNumber = ifNull($("#order_number").val());
	state = $("#state").val();
	beginDate = $("#beginDate").val();
	endDate = $("#endDate").val();
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if(beginDate == $("#beginDate").attr("placeholder")){
		beginDate = "";
	}
	if(endDate == $("#endDate").attr("placeholder")){
		endDate = "";
	}
	if(orderNumber == $("#order_number").attr("placeholder")){
		orderNumber = "";
	}
	if (checkDate()) {
		loadContent("/orderInfo/orderInfoList?pNo="+ page +"&orderNumber="+ orderNumber + "&state="+ state
				+ "&beginDate="+ beginDate+ "&endDate="+ endDate + "&pSize="+ pSize);
	}
}

//条件搜索用户列表
function getInfoList(){
	orderNumber = ifNull($("#order_number").val());
	state = $("#state").val();
	beginDate = $("#beginDate").val();
	endDate = $("#endDate").val();
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if(beginDate == $("#beginDate").attr("placeholder")){
		beginDate = "";
	}
	if(endDate == $("#endDate").attr("placeholder")){
		endDate = "";
	}
	if(orderNumber == $("#order_number").attr("placeholder")){
		orderNumber = "";
	}
	if (checkDate()) {
		loadContent("/orderInfo/orderInfoList?pNo=1&orderNumber="+ orderNumber + "&state="+ state
				+ "&beginDate="+ beginDate+ "&endDate="+ endDate + "&pSize="+ pSize);
	}
	
}
//跳转到指定页面
function gotoInfoPage(page) {
	if(page == null){
		page = $(".inputclass").val();
	}
	orderNumber = ifNull($("#order_number").val());
	state = $("#state").val();
	beginDate = $("#beginDate").val();
	endDate = $("#endDate").val();
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if(beginDate == $("#beginDate").attr("placeholder")){
		beginDate = "";
	}
	if(endDate == $("#endDate").attr("placeholder")){
		endDate = "";
	}
	if(orderNumber == $("#order_number").attr("placeholder")){
		orderNumber = "";
	}
	if (checkDate()) {
		loadContent("/orderInfo/orderInfoList?pNo="+ page +"&orderNumber="+ orderNumber + "&state="+ state
				+ "&beginDate="+ beginDate+ "&endDate="+ endDate + "&pSize="+ pSize);
	}
}

//全选和反选
function selectAll(target){    
    if(target.checked){    
        $("#tableTbody :checkbox").attr("checked", true); 
        $(target).parents("thead").siblings("tbody").children().children().each(function(){
    	    $(this).css("background","#D3F0F1");
       });
    }else{    
        $("#tableTbody :checkbox").attr("checked", false); 
        $(target).parents("thead").siblings("tbody").children().children().each(function(){
    	    $(this).css("background","");
       });
    }    
}

//鼠标滑入一行改变背景颜色
function mouseoverOne(target){
	 $(target).children().each(function(){
		  	    $(this).css("background","#D3F0F1");
		     });
}

//鼠标滑出一行恢复原来的背景颜色
function mouseoutOne(target){
	 $(target).children().each(function(){
	  	    $(this).css("background","");
	     });
}

//选择或取消选中当前记录
function selectOne(id,target){
    if(target.checked){    
        $("#order" + id).attr("checked", true);   
        $(target).parents("tr").children().each(function(){
    	    $(this).css("background","#D3F0F1");
       });
    }else{    
        $("#order" + id).attr("checked", false); 
        $(target).parents("tr").children().each(function(){
    	    $(this).css("background","");
       });
    } 
}

//订单详情页
function detailorderInfo(id){
	loadContent("/orderInfo/orderDetailInfo?orderId="+id);
}
//商品详情页
function detailgoodsInfo(id){
	loadContent("/orderInfo/orderGoodsDetailInfo?orderDetailId="+id);
}


function deleteByIds(){
	if($("img[name='cellChecked'][src*=honggou]").length == 0){
		layer.msg("请选择要删除的记录!",{
			icon: 3,
			time: 2000 //2秒关闭（如果不配置，默认是3秒）
		});
	}else{
		var delIds = [];
		    $("#tableTbody img[name='cellChecked'][src*=honggou]").each(function(){ 
		    	delIds.push($(this).attr("class")); 
		    }); 
		    var vals = delIds.join(',');//转换为逗号隔开的字符串 
			layer.confirm('确认删除?', {icon: 3, title:'提示'}, function(index){
			    //do something
				$.post(ctx+"/shopInfo/shopInfoList",{
					cellIDs:vals
				},function(data){
					data = JSON.parse(data);
					if(data.success){
						layer.msg("删除成功",{
							icon: 1,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						},function(){
							loadContent("");
						});
					} else {
						layer.msg(data.msg,{
							icon: 2,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						});
					}
				});
			    layer.close(index);
			});

		
	}
}

//实现字段较长时的隐藏于显示
function mouseOverAddr(item , addr){
	layer.tips(addr, '#addr' + item, {
		  tips: [3, '#37CED4'],
		  time: 5000
		});
}

//导出订单
function exportExcel() {
	orderNumber = ifNull($("#orderNumber").val());
	beginDate = ifNull($("#beginDate").val());
	endDate = ifNull($("#endDate").val());
	state = ifNull($("#state").val());
	total = $("#total").val();
	
	if(total >0) {
		location.href=ctx +"/orderInfo/exportOrderListExcel?orderNumber="+ orderNumber + "&beginDate="+ beginDate+ "&endDate="+ 
		endDate+ "&state="+ state;
	} else {
		layer.msg("未获取到数据，无法导出...",{
			icon: 2,
			time: 2000 //2秒关闭（如果不配置，默认是3秒）
		});
	}
	
}

//积分商品，秒杀商品发货
function sendGoods(orderId){
	layer.confirm('确认发货?', {icon: 3, title:'提示'}, function(index){
	    //do something
	    var proIndex = layer.load(1);
		$.post(ctx+"/orderInfo/sendGoods",{
			orderInfoId:orderId
		},function(data){
			data = JSON.parse(data);
			if(data.success){
				layer.msg(data.msg,{
					icon: 1,
					time: 2000 //2秒关闭（如果不配置，默认是3秒）
				},function(){
					loadContent("/orderInfo/orderInfoList");
				});
			} else {
				layer.msg(data.msg,{
					icon: 2,
					time: 2000 //2秒关闭（如果不配置，默认是3秒）
				});
			}
			layer.close(proIndex);
		});
	    layer.close(index);
	});
}

//运营中心确认退款完成
function completeRefund(orderId){
	layer.confirm('确认完成退款?', {icon: 3, title:'提示'}, function(index){
	    //do something
	    var proIndex = layer.load(1);
		$.post(ctx+"/orderInfo/completeRefund",{
			orderInfoId:orderId
		},function(data){
			data = JSON.parse(data);
			if(data.success){
				layer.msg("操作成功",{
					icon: 1,
					time: 2000 //2秒关闭（如果不配置，默认是3秒）
				},function(){
					loadContent("/orderInfo/orderInfoList");
				});
			} else {
				layer.msg(data.msg,{
					icon: 2,
					time: 2000 //2秒关闭（如果不配置，默认是3秒）
				});
			}
			 layer.close(proIndex);
		});
	    layer.close(index);
	});
}

//确认完成退货
function completeReturn(orderId){
	layer.confirm('确认完成退货?', {icon: 3, title:'提示'}, function(index){
	    //do something
	    var proIndex = layer.load(1);
		$.post(ctx+"/orderInfo/completeReturn",{
			orderInfoId:orderId
		},function(data){
			data = JSON.parse(data);
			if(data.success){
				layer.msg("操作成功",{
					icon: 1,
					time: 2000 //2秒关闭（如果不配置，默认是3秒）
				},function(){
					loadContent("/orderInfo/orderInfoList");
				});
			} else {
				layer.msg(data.msg,{
					icon: 2,
					time: 2000 //2秒关闭（如果不配置，默认是3秒）
				});
			}
			layer.close(proIndex);
		});
	    layer.close(index);
	});
}

